package uit.nfcmenu.models;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class OrderAdapter extends DatabaseAdapter {

	public OrderAdapter(Context context) {
		super(context);
	}

	public int insertOrderModel(OrderModel model) {
		int value = -1;

		this.open();

		try {
			// Assign values to add...
			ContentValues contentValues = new ContentValues();

			contentValues.put(DbDefines.TableOrder.orderID, model.getOrderID());
			contentValues.put(DbDefines.TableOrder.itemID, model.getItemID());
			contentValues.put(DbDefines.TableOrder.status, model.getStatus());
			contentValues.put(DbDefines.TableOrder.tableNo, model.getTableNo());
			contentValues.put(DbDefines.TableOrder.orderTime,
					model.getOrderTime());
			contentValues.put(DbDefines.TableOrder.quantity,
					model.getQuantity());
			contentValues.put(DbDefines.TableOrder.description,
					model.getDescription());
			contentValues.put(DbDefines.TableOrder.comment, model.getComment());

			// Add to database
			value = (int) this.mDb.insert(DbDefines.OrderModel, "",
					contentValues);
		} catch (Exception e) {

		} finally {
			this.close();
		}

		return value;
	}

	public int deleteOrderModel(int orderID) {
		int result = 0;
		this.open();
		try {
			result = this.mDb.delete(DbDefines.OrderModel,
					DbDefines.TableOrder.orderID + " = " + orderID, null);
		} finally {
			this.close();
		}
		return result;
	}

	public int updateOrderModel(OrderModel model) {
		int value = -1;

		this.open();
		try {
			// Assign values to add...
			ContentValues contentValues = new ContentValues();
			contentValues.put(DbDefines.TableOrder.status, model.getStatus());
			contentValues.put(DbDefines.TableOrder.comment, model.getComment());

			// Add to database
			value = (int) this.mDb.update(DbDefines.OrderModel, contentValues,
					DbDefines.TableOrder.orderID + " = " + model.getOrderID(),
					null);
			if (value == 0) {

				Log.i("OrderAdapter",
						"There may not model has id like that, request insert new one");
				this.close();
				return insertOrderModel(model);

			}
		} finally {
			this.close();
		}
		return value;
	}

	public OrderModel loadOrderModel(int orderID) {
		this.open();
		// getting data from database
		Cursor cursor = null;
		OrderModel model = null;
		try {
			cursor = this.mDb.query(DbDefines.OrderModel, new String[] {
					DbDefines.TableOrder.orderID, DbDefines.TableOrder.itemID,
					DbDefines.TableOrder.status, DbDefines.TableOrder.tableNo,
					DbDefines.TableOrder.orderTime,
					DbDefines.TableOrder.quantity,
					DbDefines.TableOrder.description,
					DbDefines.TableOrder.comment },
					DbDefines.TableOrder.orderID + " = " + "'" + orderID + "'",
					null, "", "", "", "1");

			if (cursor != null && cursor.getCount() > 0) {
				model = new OrderModel(cursor);
				cursor.moveToFirst();

			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return model;
	}

	public int insertOrderStatusModel(OrderModel model) {
		int value = -1;

		this.open();

		try {
			// Assign values to add...
			ContentValues contentValues = new ContentValues();

			contentValues.put(DbDefines.TableOrder.orderID, model.getOrderID());
			contentValues.put(DbDefines.TableOrder.itemID, model.getItemID());
			contentValues.put(DbDefines.TableOrder.status, model.getStatus());
			contentValues.put(DbDefines.TableOrder.tableNo, model.getTableNo());
			contentValues.put(DbDefines.TableOrder.orderTime,
					model.getOrderTime());
			contentValues.put(DbDefines.TableOrder.quantity,
					model.getQuantity());
			contentValues.put(DbDefines.TableOrder.description,
					model.getDescription());

			// Add to database
			value = (int) this.mDb.insert(DbDefines.OrderModel, "",
					contentValues);
		} finally {
			this.close();
		}

		return value;
	}

	public ArrayList<OrderModel> getOrdersInDB() {
		// TODO Auto-generated method stub
		this.open();
		// getting data from database
		Cursor cursor = null;
		OrderModel model = null;
		ArrayList<OrderModel> models = new ArrayList<OrderModel>();
		try {
			cursor = this.mDb.query(DbDefines.OrderModel, new String[] {
					DbDefines.TableOrder.orderID, DbDefines.TableOrder.itemID,
					DbDefines.TableOrder.status, DbDefines.TableOrder.tableNo,
					DbDefines.TableOrder.orderTime,
					DbDefines.TableOrder.quantity,
					DbDefines.TableOrder.description,
					DbDefines.TableOrder.comment }, "", null, "", "", "");
			if(cursor!=null){
				while (cursor.moveToNext()){
					model=new OrderModel(cursor);
					models.add(model);
				}
			}
//			cursor.moveToFirst();
//			do {
//				model = new OrderModel(cursor);
//				models.add(model);

//			} while (cursor.moveToNext());
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return models;
	}
}
